package no.rodland.acc.parsers;

import java.util.Arrays;
import java.util.List;

import no.rodland.acc.db.DbRow;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 23, 2010 2:03:54 PM
 * @Author: fmr
 */
public abstract class AbstractParser implements DbRowParser {
    protected String separator = ";";
    protected String headerRowRegexp = "";
    protected DateParser dateParser;
    protected NumberParser numberParser;
    protected TextDateParser textDateParser;

    public AbstractParser(
            TextDateParser textDateParser,
            DateParser dateParser,
            NumberParser numberParser) {
        this.textDateParser = textDateParser;
        this.dateParser = dateParser;
        this.numberParser = numberParser;
        configure();
    }

    public abstract void configure();

    @Override
    public List<DbRow> parse(String line) throws ParseException {
        if (!isParseble(line)) {
            return null;
        }
        return parseLine(line);
    }

    protected abstract <T extends DbRow> List<T> parseLine(String line) throws ParseException;

    @Override
    public boolean isParseble(String line) {
        return line != null && line.length() > 0;
    }

    protected static <T extends DbRow> List<T> makeList(T... row) {
        return Arrays.asList(row);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("AbstractParser");
        sb.append("{name: ").append(getName()).append("}");
        return sb.toString();
    }

    public void setSeparator(String separator) {
        this.separator = separator;
    }
}
